package dao;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-11-07
 * Time: 22:56
 */
public class DButi{
    private static volatile DataSource source = null;

    public DataSource getSource(){
        if(source == null){
            synchronized (DButi.class){
                if(source == null){
                    source = new MysqlDataSource();
                    ((MysqlDataSource)source).setUrl("jdbc:mysql://127.0.0.1:3306/java_blog_system?useSSL=false&characterEncoding=utf8");
                    ((MysqlDataSource)source).setUser("root");
                    ((MysqlDataSource)source).setPassword("123456");
                    //((MysqlDataSource)source).setPassword("");
                }
            }
        }
        return source;
    }

    public Connection getconnect() throws SQLException {
        return getSource().getConnection();
    }

    public void close(Connection connection, PreparedStatement statement, ResultSet resultSet) throws SQLException {
        if(connection != null){
            connection.close();
        }
        if (statement != null){
            statement.close();
        }
        if (resultSet != null){
            resultSet.close();
        }
    }

}
